import java.util.ArrayList;
import java.util.Scanner;

public class StudentManageSystem {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        ArrayList<Student> list=new ArrayList<>();
        loop:while (true) {
            System.out.println("-------------欢迎来到学生管理系统-------------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入您的选择");
            String choice=sc.next();
            switch (choice) {
                case "1":
                    System.out.println("添加学生");
                    addStudent(list, sc);

                    break;
                case "2":
                    System.out.println("删除学生");
                    deleteStudent(list, sc);
                    break;
                case "3":
                    System.out.println("修改学生");
                    updateStudent(list, sc);
                    break;
                case "4":
                    System.out.println("查询学生");
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("系统已退出");
                    break loop;
                //System.exit(0)  停止虚拟机运行
                default:
                    System.out.println("选择错误,请重新选择");
            }
        }
        sc.close();
    }

    //添加学生
    public static void addStudent(ArrayList<Student>list,Scanner sc){
        String id= null;
        while (true) {
            System.out.println("请输入需要添加的学生id");
            id = sc.next();
            if(contain(list,id)){
                System.out.println("该学生已存在,请重新输入");
                continue;
            }else{
                break;
            }
        }
        System.out.println("请输入需要添加的学生姓名");
        String name=sc.next();
        System.out.println("请输入需要添加的学生年龄");
        int age=sc.nextInt();
        System.out.println("请输入需要添加的学生地址");
        String address=sc.next();
        Student student=new Student(id,name,age,address);
        list.add(student);
        System.out.println("数据添加成功");
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student>list,Scanner sc){
        System.out.println("请输入需要删除的学生id");
        String id=sc.next();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                list.remove(i);
                System.out.println("已删除该学生信息");
                return ;
            }
        }
        System.out.println("不存在该学生,请重试");
        return ;
    }

    //修改学生
    public static void updateStudent(ArrayList<Student>list,Scanner sc){
        System.out.println("请输入需要修改的学生id");
        String id=sc.next();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                System.out.println("请输入需要修改的学生姓名");
                String name=sc.next();
                System.out.println("请输入需要修改的学生年龄");
                int age=sc.nextInt();
                System.out.println("请输入需要修改的学生地址");
                String address=sc.next();
                list.get(i).setAge(age);
                list.get(i).setName(name);
                list.get(i).setAddress(address);
                System.out.println("数据修改成功");
                return ;
            }
        }
        System.out.println("id不存在,请重试");
        return ;
    }

    //查询学生
    public static void queryStudent(ArrayList<Student>list){
        if(list.size()==0){
            System.out.println("当前无学生信息,请添加后再查询");
        }else{
            System.out.println("id     姓名        年龄      家庭地址");
            for (int i = 0; i < list.size(); i++) {
                Student s=list.get(i);
                System.out.println(s.getId()+"     "+s.getName()+"        "+s.getAge()+"      "+s.getAddress());
            }
        }

    }

    //判断学生id是否在表中
    public static boolean contain(ArrayList<Student>list,String id){
        for (int i = 0; i < list.size() ; i++) {
            if(list.get(i).getId().equals(id)){
                return true;
            }
        }
        return false;
    }
}
